package botlab.gui.library;

import botlab.Game;
import botlab.gui.GUI;
import com.Ostermiller.Syntax.HighlightedDocument;
import java.awt.event.ItemEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

/**
 *
 * @author Quentin
 */
public class LibraryFrame extends javax.swing.JFrame
{

    private GUI gui;

    /**
     * Creates new form LibraryFrame
     */
    public LibraryFrame(GUI gui)
    {
        this.gui = gui;
        initComponents();
        String textIni = solutionEditorPane.getText();
        HighlightedDocument hDoc = new HighlightedDocument();
        hDoc.setHighlightStyle(HighlightedDocument.JAVA_STYLE);
        solutionEditorPane.setDocument(hDoc);
        solutionEditorPane.setText(textIni);
        try
        {
            functionsEditorPane.setPage(LibraryFrame.class.getResource(
                    "/docs/botlab/doc.html"));
            functionsEditorPane.addHyperlinkListener(new HyperlinkListener() {
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
                        try {
                            functionsEditorPane.setPage(e.getURL());
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }

                }
            });
        
        } catch (IOException ex)
        {
            Logger.getLogger(LibraryFrame.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        functionsEditorPane = new javax.swing.JEditorPane();
        jPanel3 = new javax.swing.JPanel();
        solutionCheckBox = new javax.swing.JCheckBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        solutionEditorPane = new javax.swing.JTextPane();
        copySolutionButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTabbedPane1.setMinimumSize(new java.awt.Dimension(600, 300));

        functionsEditorPane.setEditable(false);
        functionsEditorPane.setPreferredSize(new java.awt.Dimension(700, 300));
        jScrollPane1.setViewportView(functionsEditorPane);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 795, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("functions", jPanel2);

        solutionCheckBox.setText("show a solution");
        solutionCheckBox.setEnabled(false);
        solutionCheckBox.addItemListener(new java.awt.event.ItemListener()
        {
            public void itemStateChanged(java.awt.event.ItemEvent evt)
            {
                solutionCheckBoxItemStateChanged(evt);
            }
        });

        solutionEditorPane.setEditable(false);
        solutionEditorPane.setText("No level selected.");
        jScrollPane3.setViewportView(solutionEditorPane);

        copySolutionButton.setText("copy to editor");
        copySolutionButton.setEnabled(false);
        copySolutionButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                copySolutionButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(solutionCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 597, Short.MAX_VALUE)
                .addComponent(copySolutionButton))
            .addComponent(jScrollPane3)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(solutionCheckBox)
                    .addComponent(copySolutionButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("solution", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void copySolutionButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_copySolutionButtonActionPerformed
    {//GEN-HEADEREND:event_copySolutionButtonActionPerformed
        this.gui.getEditorPane().setText(solutionEditorPane.getText());
    }//GEN-LAST:event_copySolutionButtonActionPerformed

    private void solutionCheckBoxItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_solutionCheckBoxItemStateChanged
    {//GEN-HEADEREND:event_solutionCheckBoxItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED)
        {
            solutionEditorPane.setText(
                Game.getApp().getCurrentLevel().getSolution());
            copySolutionButton.setEnabled(true);
        } else
        {
            solutionEditorPane.setText("");
            copySolutionButton.setEnabled(false);
        }
    }//GEN-LAST:event_solutionCheckBoxItemStateChanged

    public void levelSelected()
    {
        solutionCheckBox.setEnabled(true);
        solutionEditorPane.setText("");
    }
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[])
//    {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try
//        {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
//            {
//                if ("Nimbus".equals(info.getName()))
//                {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex)
//        {
//            java.util.logging.Logger.getLogger(LibraryFrame.class.getName()).log(
//                    java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex)
//        {
//            java.util.logging.Logger.getLogger(LibraryFrame.class.getName()).log(
//                    java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex)
//        {
//            java.util.logging.Logger.getLogger(LibraryFrame.class.getName()).log(
//                    java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex)
//        {
//            java.util.logging.Logger.getLogger(LibraryFrame.class.getName()).log(
//                    java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable()
//        {
//            public void run()
//            {
//                new LibraryFrame().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton copySolutionButton;
    private javax.swing.JEditorPane functionsEditorPane;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JCheckBox solutionCheckBox;
    private javax.swing.JTextPane solutionEditorPane;
    // End of variables declaration//GEN-END:variables
}
